const config = require('./config/config.json').development;
const { Sequelize } = require('sequelize');

async function checkData() {
  try {
    const sequelize = new Sequelize(config.database, config.username, config.password, {
      host: config.host,
      dialect: config.dialect
    });

    await sequelize.authenticate();
    console.log('Connection has been established successfully.');

    // 检查批次数据
    const [batchResults] = await sequelize.query(
      'SELECT batch_id, data_type, status, processed_count, failed_count FROM plugin_data_batches ORDER BY created_at DESC LIMIT 5'
    );
    
    console.log('Recent plugin data batches:');
    batchResults.forEach((item, index) => {
      console.log(`${index + 1}. Batch ID: ${item.batch_id}, Data Type: ${item.data_type}, Status: ${item.status}, Processed: ${item.processed_count}, Failed: ${item.failed_count}`);
    });

    // 检查销售订单相关数据
    const [orderResults] = await sequelize.query(
      "SELECT item_id, batch_id, category, status, error_message FROM plugin_data_items WHERE category LIKE '%order%' ORDER BY created_at DESC LIMIT 5"
    );
    
    console.log('\nRecent order-related items:');
    orderResults.forEach((item, index) => {
      console.log(`${index + 1}. Item ID: ${item.item_id}, Batch ID: ${item.batch_id}, Category: ${item.category}, Status: ${item.status}, Error: ${item.error_message || 'None'}`);
    });

    // 检查销售订单表
    try {
      const [salesOrderResults] = await sequelize.query('SELECT COUNT(*) as count FROM sales_orders');
      console.log(`\nSales orders table count: ${salesOrderResults[0].count}`);
    } catch (err) {
      console.log('\nSales orders table: Not found or error');
    }

    // 检查销售订单详情表
    try {
      const [salesOrderItemResults] = await sequelize.query('SELECT COUNT(*) as count FROM sales_order_items');
      console.log(`Sales order items table count: ${salesOrderItemResults[0].count}`);
    } catch (err) {
      console.log('Sales order items table: Not found or error');
    }

    await sequelize.close();
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
}

checkData();